package com.chinaunicom.emergency.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author liliang
 * @date 2019-12-13
 */
public class SQLUtil {

    public static String getWhere(Map where){
        Iterator it = where.entrySet().iterator();
        StringBuffer values = new StringBuffer("");
        while (it.hasNext()){
            Map.Entry entry = (Map.Entry)it.next();
            String key = (String)entry.getKey();
            Object value =  entry.getValue();
            if(value instanceof String){
                if(key.indexOf("<") != -1 ||
                        key.indexOf(">") != -1||
                        key.indexOf("<=") != -1||
                        key.indexOf(">=") != -1||
                        key.indexOf("=") != -1 ||
                        key.indexOf("like") != -1){
                    values.append(" and "+key + " '"+value+"' ");
                }else {
                    values.append(" and "+key + "='" + value + "'");
                }
            }else if(value instanceof Integer){
                if(key.indexOf("<") != -1 ||
                        key.indexOf(">") != -1||
                        key.indexOf("<=") != -1||
                        key.indexOf(">=") != -1||
                        key.indexOf("=") != -1){
                    values.append(" and "+key + value);
                }else{
                    values.append(" and "+key + "=" + value);
                }
            }else if(value instanceof List){
                StringBuffer sb = new StringBuffer();
                for(Object obj : (List)value){
                    if(obj instanceof String){
                        sb.append("'" + obj + "',");
                    }else if (obj instanceof Integer){
                        sb.append(obj + ",");
                    }
                }
                if(sb.length() > 0){
                    sb.setLength(sb.length()-1);
                }
                values.append(" and "+key + " in (" + sb + ")");
            }
        }
        return values.toString();
    }
}
